package com.sls.web.manager.game.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.web.manager.game.constants.ResultCode;
import com.sls.web.manager.game.pojo.ekhui.Account;
import com.sls.web.manager.game.pojo.ekhui.Area;
import com.sls.web.manager.game.pojo.ekhui.City;
import com.sls.web.manager.game.pojo.ekhui.Operator;
import com.sls.web.manager.game.pojo.ekhui.Province;
import com.sls.web.manager.game.pojo.ekhui.Trader;
import com.sls.web.manager.pojo.UserInfoVO;

public class ResisterClient {
	private static Logger log = LoggerFactory.getLogger(ResisterClient.class) ;
	private final static Lock lock = new ReentrantLock() ;
	private volatile static ResisterClient instance = null ;
	private ResisterClient(){
		super() ;
	}
	public static ResisterClient getInstance(){
		if(instance == null){
			try {
				lock.lock();
				if (instance == null) {
					instance = new ResisterClient();
				}
			} finally{
				lock.unlock() ;
			}
		}
		return  instance ;
	}
	public List<Province> getProvinceList(UserInfoVO userInfoVO){
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		head.setServiceCode("300024");
		head.setRequestId(UUID.randomUUID().toString());
		SocketDTO socketDTO = SocketUtil.send(head, null);
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
			String body = socketDTO.getBody();
			Gson gson = new Gson();
			return gson.fromJson(body, new TypeToken<List<Province>>(){}.getType()) ;
		}
		return null ;
	}
	public List<City> getCityList(UserInfoVO userInfoVO , Province province){
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		head.setServiceCode("300025");
		head.setRequestId(UUID.randomUUID().toString());
		SocketDTO socketDTO = SocketUtil.send(head, province);
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
			String body = socketDTO.getBody();
			Gson gson = new Gson();
			return gson.fromJson(body, new TypeToken<List<City>>(){}.getType()) ;
		}
		return null ;
	}
	public List<Area> getAreaList(UserInfoVO userInfoVO , City city){
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		head.setServiceCode("300026");
		head.setRequestId(UUID.randomUUID().toString());
		SocketDTO socketDTO = SocketUtil.send(head, city);
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
			String body = socketDTO.getBody();
			Gson gson = new Gson();
			return gson.fromJson(body, new TypeToken<List<Area>>(){}.getType()) ;
		}
		return null ;
	}
	public boolean register(UserInfoVO userInfoVO , Trader trader , Operator operator , Account account){
		Map<String , Object> paramMap = new HashMap<String , Object>() ;
		paramMap.put("trader", trader) ;
		paramMap.put("operator", operator) ;
		paramMap.put("account", account) ;
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		head.setServiceCode("300028");
		head.setRequestId(UUID.randomUUID().toString());
		SocketDTO socketDTO = SocketUtil.send(head, paramMap);
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
			String body = socketDTO.getBody();
			Gson gson = new Gson();
			return gson.fromJson(body, new TypeToken<Boolean>(){}.getType()) ;
		}
		return false ;
	}
}
